package com.chain.qs.modules.biz.web;

import com.chain.qs.common.config.Global;
import com.chain.qs.common.utils.StringUtils;
import com.chain.qs.common.web.BaseController;
import com.chain.qs.modules.biz.entity.Station;
import com.chain.qs.modules.biz.service.StationService;
import com.chain.qs.modules.sys.entity.Office;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;


/**
 * 发射站Controller
 *
 * @author Wang zhiping
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/station")
public class StationController extends BaseController {

    @Resource
    private StationService stationService;

    @ModelAttribute
    public Station get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return stationService.get(id);
        } else {
            return new Station();
        }
    }

    @RequestMapping(value = {"list", ""})
    public String list(Model model) {
        model.addAttribute("list", stationService.findListWithOffice(new Office()));
        return "modules/biz/stationList";
    }

    @RequestMapping(value = "form")
    public String form(Station station) {
//        model.addAttribute("equipmentGroups",groupService.findList(new EquipmentGroup()));
        return "modules/biz/stationForm";
    }

    @ResponseBody
    @RequestMapping(value = "save")//@Valid
    public String save(Station station, RedirectAttributes redirectAttributes) {
        stationService.save(station);

        addMessage(redirectAttributes, "保存发射站:'" + station.getName() + "'成功");
        //return "redirect:" + adminPath + "/biz/station/list";
        return "success";
    }


    @RequestMapping(value = "delete")
    public String delete(Station station, RedirectAttributes redirectAttributes) {
        if (Global.isDemoMode()) {
            addMessage(redirectAttributes, "演示模式，不允许操作！");
            return "redirect:" + adminPath + "/biz/station/";
        }
        stationService.delete(station);
        addMessage(redirectAttributes, "删除站点成功");
        return "redirect:" + adminPath + "/biz/station/";
    }

    @RequestMapping(value = {"group"})
    public String group(Model model) {
        model.addAttribute("list", stationService.findListWithOffice(new Office()));
        return "modules/biz/sGroupList";
    }

}
